// 04myCenter/03receiveAddress/01newReceiveAddress/newReceiveAddress.js
var co = require('../../../resource/js/common.js')
var map = require('../../../resource/js/map.js')
var app = getApp()
var _this, _data, _opt, lat = {} //经纬度
var p = 0,
   c = 0,
   d = 0
Page({
   data: {
      showMessage: false, //保存地址时候是否符合规格
      messageContent: '', //保存时未满足要求提示语
      // showDistpicker: false,
      Address_ID: '',
      inprovince: '',
      inprovinceId: '',
      incity: '',
      incityId: '',
      indistrict: '',
      Address_Is_Default: 1,
      name: '', //显示的名字
      tel: '', //显示的电话
      wd: '', //纬度
      js: '', //经度
      addressCode: '', //地区编号
      thisAddress: '', //当前编辑地址详情
   },
   onLoad: function (options) {
      _this = this
      _data = this.data
   },

   //选择所在位置
   selectAddress(e) {
      wx.chooseLocation({
         success(res) {
            console.log("【允许授权】===", res)
            map.getLocationName(res, function (res2) {
               //console.log('res2======',res2)
               var str = res2.location.result
               _this.setData({
                  detail_info: str.address_component.street_number,
                  wd: str.ad_info.location.lat,
                  jd: str.ad_info.location.lng,
                  inprovince: str.address_component.province,
                  incity: str.address_component.city,
                  indistrict: str.address_component.district
               })
               _this.getPuaCode()
            })
         },
         fail: function (res) {
            if (res.errMsg.indexOf('auth') != -1) {
               wx.openSetting({
                  success: function (res) {
                     wx.setStorageSync("noMap", 1)
                  }
               })
            }
         }
      })
   },

   //提交添加
   addPersonalNewAddress: function (e) {
      //console.log('bizDetail===', _data.bizDetail)
      var info = e.detail.value
      var telRule = /^1[0-9]{10}$/
      console.log('info==', info)
      var url = 'address/Addrmg/saveAddress'
      if (info.Receiver_Name == '') {
         this.showMessage('请输入收货人名称')
      } else if (info.Address_Mobile == '') {
         this.showMessage('请输入手机号码')
      } else if (!telRule.test(info.Address_Mobile)) {
         this.showMessage('手机号码格式不正确')
      } else if (info.Address_Province == '') {
         this.showMessage('请选择所在省')
      } else if (info.Address_City == '') {
         this.showMessage('请选择所在市')
      } else if (info.Address_Area == '' && this.info.districtCode.length != 0) {
         this.showMessage('请选择所在地区')
      } else if (info.Address_Detailed == '') {
         this.showMessage('请输入详细地址')
      }else{
         var param = {
            User_ID: app.Data.userInfo.User_ID,
            Address_Mobile: info.Address_Mobile,
            Receiver_Name: info.Receiver_Name,
            Addr_PrimaryLng: _data.jd,
            Addr_PrimaryLat: _data.wd,
            Address_Province: _data.addressCode.PID,
            Address_City: _data.addressCode.CID,
            Address_Area: _data.addressCode.AID,
            Address_Detailed: info.Address_Detailed,
            Address_Is_Default: info.Address_Is_Default,
            Users_ID: 'jyhs'
         }
         console.log('param', param)
         co.getShareApi(url, param, function (res) {
            console.log('res===', res)
            if (res.code == 0) {
               _this.showMessage(' 添加成功')
               setTimeout(function () {
                  var pages = getCurrentPages(),
                     prevPage = pages[pages.length - 2];
                  prevPage.setData({
                     alter: "123"
                  })
                  wx.navigateBack({})
               }, 1000)
            }
         })
      }      
   },

   //显示提示信息
   showMessage: function (text) {
      var that = this
      that.setData({
         showMessage: true,
         messageContent: text
      })
      setTimeout(function () {
         that.setData({
            showMessage: true,
            messageContent: text
         })
         setTimeout(function () {
            that.setData({
               showMessage: false,
               messageContent: ''
            })
         }, 3000)
      })
   },
   //获取省市区编号
   getPuaCode: function () {
      var url = 'api/Areas/getPCACode'
      var param = {
         Province_Name: _data.inprovince,
         City_Name: _data.incity,
         Area_Name: _data.indistrict
      }
      co.getShareApi(url, param, function (res) {
         if (res.code == 0) {
            _this.setData({
               addressCode: res.data
            })
         }
      })
   },
})